home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 March / MINDWARE_MAR_2001.iso / Software / developers / Readers / buildzone zapper / Zapper.exe / ZAPPER.EXE / HTML / ZAPPERINVOKERSCRIPT
Encoding:
Text File  |  2000-10-15  |  4.9 KB  |  184 lines

  1. // 1/6/00 VERSION 2
  2. <SCRIPT LANGUAGE="JavaScript" defer>
  3.  
  4. function ShowZapper(strSelection, strContext, index)
  5. {
  6.     var Zapper = new ActiveXObject("Zapper.GLZapper");
  7.     Zapper.Text = strSelection;
  8.     Zapper.Context = strContext;
  9.     Zapper.Index = index;
  10.     Zapper.OpenZapper();
  11. }
  12.  
  13. function FindTextAndShowZapper()
  14. {
  15.     var parentwin = external.menuArguments;
  16.     var doc = parentwin.document;
  17.     
  18.     //declare empty variable
  19.     strSelection = new String("");
  20.     strContext   = new String("");
  21.     var index    = -1;
  22.  
  23.     var sel = doc.selection;
  24.     if (sel == 0)
  25.     {
  26.         ShowZapper(strSelection, strContext, index);
  27.         return;
  28.     }
  29.     var rng = sel.createRange();
  30.     if (rng == 0)
  31.     {
  32.         ShowZapper(strSelection, strContext, index);
  33.         return;
  34.     }
  35.         
  36.     // Get the text from the selection:    
  37.     strSelection = new String(rng.text);
  38.     
  39.     var element = rng.parentElement();
  40.  
  41.     // if the tag's name is IMG - no need to present it, just get out
  42.     if ( (element != 0) && (element.tagName != "IMG"))
  43.     {
  44.         //if it's a font kind tag (bold, italic...), go up to the FONT parent's parent,
  45.         //and continue from there:
  46.         elementTagName = new String(element.tagName);
  47.         if ((elementTagName == "I") || (elementTagName == "B") ||
  48.             (elementTagName == "U") || (elementTagName == "S"))
  49.         {
  50.             do
  51.             {
  52.                 if (element.parentElement != 0)
  53.                     element = element.parentElement;
  54.                 elementTagName = element.tagName;
  55.             }
  56.             while(elementTagName == "FONT");
  57.  
  58.             var tempElement = element.parentElement;
  59.             //don't go to the object's parent if it's a link
  60.             if (tempElement.tagName != "A")
  61.                 element = tempElement;
  62.         }
  63.  
  64.         var context = doc.body.createTextRange();
  65.         context.moveToElementText(element);
  66.  
  67.         strContext = new String(context.text);
  68.  
  69.         //if Context is empty and Selection is not empty, so try and get the Context out of the IEAutomator
  70.         if ( strContext.empty && !strSelection.empty)
  71.         {
  72.             ShowZapper(strSelection, strContext, index);
  73.             return;
  74.         }
  75.  
  76.         // Compute index as the length of the part of the context before
  77.         // the beginning of the selection
  78.         context.setEndPoint("EndToStart", rng);
  79.         index = context.text.length;
  80.  
  81.         // Get previous and next siblings of pElement, by iterating on pElement's
  82.         // parent's children collection.
  83.         // Unfortunately, the prevSibling and nextSibling methods were introduced only
  84.         // in IE5.
  85.         var parentToElement = element.parentElement;
  86.         if ( parentToElement != 0 )
  87.         {    
  88.             //make sure parentToElement has children container
  89.             while(parentToElement.tagName == "FONT")            
  90.             {
  91.                 if (parentToElement.parentElement != 0)
  92.                     parentToElement = parentToElement.parentElement;
  93.                 else
  94.                     break;
  95.             }
  96.             var siblings = parentToElement.children;
  97.             var prevElement = 0;
  98.             var nextElement = 0;
  99.             var i = 0;
  100.             for (i = 0; i < siblings.length; i++)
  101.             {
  102.                 var p = siblings.item(i);
  103.                 if (p == element)
  104.                 {
  105.                     break;
  106.                 
  107.                 }
  108.                 prevElement = p;
  109.             }
  110.             if (i < siblings.length - 1)
  111.                 nextElement = siblings.item(i + 1);
  112.             if (i >= siblings.length)
  113.             {
  114.                 // What???  pElement is not a child of its own parent???
  115.                 // This shouldn't happen!
  116.                 prevElement = nextElement = 0;
  117.             }
  118.  
  119.             // add previous element to context:
  120.             if (prevElement)
  121.             {
  122.                 strPrevText = new String();
  123.                 // if it has kids, take the text from the last child:
  124.                 var children = prevElement.children;
  125.                 if (children != 0 && children.length)
  126.                 {
  127.                     var prevChild = children.item(children.length - 1);
  128.                     if (prevChild != 0)
  129.                     {
  130.                         //if tag is a script tag (contain '!' in the tagName) than there is no innerText
  131.                         //same for BASE tag
  132.                         index = (prevChild.tagName).indexOf("!",0);
  133.                         if ( (index != 0) && (prevChild.tagName != "BASE") )
  134.                             strPrevText = prevChild.innerText;
  135.                     }
  136.                 }
  137.                 // if it doesn't have kids, take the whole element's text
  138.                 if (strPrevText.empty)
  139.                     strPrevText = prevElement.innerText;
  140.  
  141.                 spaceStr = new String(" ");
  142.                 strContext = strPrevText + spaceStr + strContext;
  143.                 index += strPrevText.length + 1;
  144.             }
  145.  
  146.             // add next element to context:
  147.             if (nextElement)
  148.             {
  149.                 strNextText = new String();
  150.                 //if it has kids, take the text from the first child:
  151.                 var children = nextElement.children;
  152.                 if (children != 0 && children.length)
  153.                 {
  154.                     var nextChild = children.item(0);
  155.                     if (nextChild != 0)
  156.                     {
  157.                         //if tag is a script tag (contain '!' in the tagName) than there is no innerText
  158.                         //same for BASE tag
  159.                         index = (nextChild.tagName).indexOf("!",0);
  160.                         if ( (index != 0) && (nextChild.tagName != "BASE") )
  161.                             strNextText = nextChild.innerText;
  162.                     }
  163.                 }
  164.                 //if it doesn't have kids, take the whole element's text
  165.                 if (strNextText.empty)
  166.                     strNextText = nextElement.innerText;
  167.  
  168.                 var spaceStr = new String(" ");
  169.                 strContext = strContext + spaceStr + strNextText;
  170.             }
  171.         }
  172.     }
  173.     
  174.     ShowZapper(strSelection, strContext, index);
  175. }
  176.  
  177.     FindTextAndShowZapper();    
  178.  
  179. </SCRIPT> 
  180.  
  181.  
  182.  
  183.  
  184.